package com.uzai.console.service.stats.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.api.internal.toplink.embedded.websocket.util.StringUtil;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.stats.OrderProfitStatsDto;
import com.uzai.console.dto.stats.OrderProfitStatsQuery;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.dto.wechatwork.devicewxworkinfo.DeviceWxworkInfoQuery;
import com.uzai.console.dto.wxpubmanage.WxpubInfoListQuery;
import com.uzai.console.entity.DeviceWechatInfo;
import com.uzai.console.entity.DeviceWxworkInfo;
import com.uzai.console.entity.wxpubmanage.WxpubInfoEntity;
import com.uzai.console.mapper.DeviceWechatInfoMapper;
import com.uzai.console.mapper.DeviceWxworkInfoMapper;
import com.uzai.console.mapper.WxpubInfoMapper;
import com.uzai.console.mapper.stats.OrderProfitStatsMapper;
import com.uzai.console.service.stats.OrderProfitService;
import com.uzai.console.vo.stats.orderprofit.OrderProfitPageVo;
import com.uzai.console.vo.stats.orderprofit.OrderProfitStatsVo;
import com.uzai.console.vo.stats.orderprofit.OrderProfitVo;
import com.uzai.console.vo.takebal.redpacket.RedPacketInfoVo;
import com.uzai.console.vo.takebal.usertakebal.UserTakeBalPageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

/**
 * @author liuqi
 * @date 2021年08月25日 20:18
 */
@Service
public class OrderProfitServiceImpl implements OrderProfitService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderProfitStatsMapper orderProfitStatsMapper;
    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private DeviceWxworkInfoMapper deviceWxworkInfoMapper;
    @Autowired
    private WxpubInfoMapper wxpubInfoMapper;

    /**
     * 查询订单收益统计
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param orderProfitStatsDto
     * @return null
     */
    public OrderProfitStatsVo findOrderProfitStats(OrderProfitStatsDto orderProfitStatsDto){

        Long merId = orderProfitStatsDto.getMerId();

        if(merId == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "系统参数错误");
        }

        OrderProfitStatsVo profitVo = new OrderProfitStatsVo();
        //选择了平台
        if(StringUtils.isNotBlank(orderProfitStatsDto.getPlatform()) && !"all".equals(orderProfitStatsDto.getPlatform())){
            //淘宝
            if("tb".equals(orderProfitStatsDto.getPlatform())){
                //淘宝利润
                //总有效订单笔数
                orderProfitStatsDto.setOrderType(1);
                OrderProfitVo tbOrderProfitVo_valid = orderProfitStatsMapper.tbOrderProfitStats(orderProfitStatsDto);
                if(tbOrderProfitVo_valid != null){
                    //总佣金
                    profitVo.setCommission(Tools.getDouble(tbOrderProfitVo_valid.getCommission()));
                    //总利润
                    profitVo.setProfit(Tools.getDouble(tbOrderProfitVo_valid.getProfit()));
                    //买家分佣
                    profitVo.setCommissionUser(Tools.getDouble(tbOrderProfitVo_valid.getReUserBal()));
                    //总订单笔数
                    profitVo.setCount(Tools.getInteger(tbOrderProfitVo_valid.getCount()));
                    //总有效订单笔数
                    profitVo.setCountValid(Tools.getInteger(tbOrderProfitVo_valid.getCount()));
                    profitVo.setProfitValid(Tools.getDouble(tbOrderProfitVo_valid.getProfit()));
                }

                //总失效订单笔数
                orderProfitStatsDto.setOrderType(0);
                OrderProfitVo tbOrderProfitVo_invalid = orderProfitStatsMapper.tbOrderProfitStats(orderProfitStatsDto);
                if(tbOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    profitVo.setCountInvalid(Tools.getInteger(tbOrderProfitVo_invalid.getCount()));
                    profitVo.setProfitInvalid(Tools.getDouble(tbOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto.setOrderType(2);
                OrderProfitVo tbOrderProfitVo_refund = orderProfitStatsMapper.tbOrderProfitStats(orderProfitStatsDto);
                if(tbOrderProfitVo_refund != null){
                    //总维权订单笔数
                    profitVo.setCountRefund(Tools.getInteger(tbOrderProfitVo_refund.getCount()));
                    profitVo.setProfitRefund(Tools.getDouble(tbOrderProfitVo_refund.getProfit()));
                }

            }else if("jd".equals(orderProfitStatsDto.getPlatform())){
                //总有效订单笔数
                orderProfitStatsDto.setOrderType(1);
                OrderProfitVo jdOrderProfitVo_valid = orderProfitStatsMapper.jdOrderProfitStats(orderProfitStatsDto);
                if(jdOrderProfitVo_valid != null){
                    //总佣金
                    profitVo.setCommission(Tools.getDouble(jdOrderProfitVo_valid.getCommission()));
                    //总利润
                    profitVo.setProfit(Tools.getDouble(jdOrderProfitVo_valid.getProfit()));
                    //买家分佣
                    profitVo.setCommissionUser(Tools.getDouble(jdOrderProfitVo_valid.getReUserBal()));
                    //总订单笔数
                    profitVo.setCount(Tools.getInteger(jdOrderProfitVo_valid.getCount()));
                    //总有效订单笔数
                    profitVo.setCountValid(Tools.getInteger(jdOrderProfitVo_valid.getCount()));
                    profitVo.setProfitValid(Tools.getDouble(jdOrderProfitVo_valid.getProfit()));
                }

                //总失效订单笔数
                orderProfitStatsDto.setOrderType(0);
                OrderProfitVo jdOrderProfitVo_invalid = orderProfitStatsMapper.jdOrderProfitStats(orderProfitStatsDto);
                if(jdOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    profitVo.setCountInvalid(Tools.getInteger(jdOrderProfitVo_invalid.getCount()));
                    profitVo.setProfitInvalid(Tools.getDouble(jdOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto.setOrderType(2);
                OrderProfitVo jdOrderProfitVo_refund = orderProfitStatsMapper.jdOrderProfitStats(orderProfitStatsDto);
                if(jdOrderProfitVo_refund != null){
                    //总维权订单笔数
                    profitVo.setCountRefund(Tools.getInteger(jdOrderProfitVo_refund.getCount()));
                    profitVo.setProfitRefund(Tools.getDouble(jdOrderProfitVo_refund.getProfit()));
                }
            }else if("pdd".equals(orderProfitStatsDto.getPlatform())){
                //拼多多利润
                //总有效订单笔数
                orderProfitStatsDto.setOrderType(1);
                OrderProfitVo pddOrderProfitVo_valid = orderProfitStatsMapper.pddOrderProfitStats(orderProfitStatsDto);
                if(pddOrderProfitVo_valid != null){
                    //总佣金
                    profitVo.setCommission(Tools.getDouble(pddOrderProfitVo_valid.getCommission()));
                    //总利润
                    profitVo.setProfit(Tools.getDouble(pddOrderProfitVo_valid.getProfit()));
                    //买家分佣
                    profitVo.setCommissionUser(Tools.getDouble(pddOrderProfitVo_valid.getReUserBal()));
                    //总订单笔数
                    profitVo.setCount(Tools.getInteger(pddOrderProfitVo_valid.getCount()));
                    //总有效订单笔数
                    profitVo.setCountValid(Tools.getInteger(pddOrderProfitVo_valid.getCount()));
                    profitVo.setProfitValid(Tools.getDouble(pddOrderProfitVo_valid.getProfit()));
                }

                //总失效订单笔数
                orderProfitStatsDto.setOrderType(0);
                OrderProfitVo pddOrderProfitVo_invalid = orderProfitStatsMapper.pddOrderProfitStats(orderProfitStatsDto);
                if(pddOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    profitVo.setCountInvalid(Tools.getInteger(pddOrderProfitVo_invalid.getCount()));
                    profitVo.setProfitInvalid(Tools.getDouble(pddOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto.setOrderType(2);
                OrderProfitVo pddOrderProfitVo_refund = orderProfitStatsMapper.pddOrderProfitStats(orderProfitStatsDto);
                if(pddOrderProfitVo_refund != null){
                    //总维权订单笔数
                    profitVo.setCountRefund(Tools.getInteger(pddOrderProfitVo_refund.getCount()));
                    profitVo.setProfitRefund(Tools.getDouble(pddOrderProfitVo_refund.getProfit()));
                }
            }else if("vph".equals(orderProfitStatsDto.getPlatform())){
                //唯品会利润
                //总有效订单笔数
                orderProfitStatsDto.setOrderType(1);
                OrderProfitVo vphOrderProfitVo_valid = orderProfitStatsMapper.vphOrderProfitStats(orderProfitStatsDto);
                if(vphOrderProfitVo_valid != null){
                    //总佣金
                    profitVo.setCommission(Tools.getDouble(vphOrderProfitVo_valid.getCommission()));
                    //总利润
                    profitVo.setProfit(Tools.getDouble(vphOrderProfitVo_valid.getProfit()));
                    //买家分佣
                    profitVo.setCommissionUser(Tools.getDouble(vphOrderProfitVo_valid.getReUserBal()));
                    //总订单笔数
                    profitVo.setCount(Tools.getInteger(vphOrderProfitVo_valid.getCount()));
                    //总有效订单笔数
                    profitVo.setCountValid(Tools.getInteger(vphOrderProfitVo_valid.getCount()));
                    profitVo.setProfitValid(Tools.getDouble(vphOrderProfitVo_valid.getProfit()));
                }

                //总失效订单笔数
                orderProfitStatsDto.setOrderType(0);
                OrderProfitVo vphOrderProfitVo_invalid = orderProfitStatsMapper.vphOrderProfitStats(orderProfitStatsDto);
                if(vphOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    profitVo.setCountInvalid(Tools.getInteger(vphOrderProfitVo_invalid.getCount()));
                    profitVo.setProfitInvalid(Tools.getDouble(vphOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto.setOrderType(2);
                OrderProfitVo vphOrderProfitVo_refund = orderProfitStatsMapper.vphOrderProfitStats(orderProfitStatsDto);
                if(vphOrderProfitVo_refund != null){
                    //总维权订单笔数
                    profitVo.setCountRefund(Tools.getInteger(vphOrderProfitVo_refund.getCount()));
                    profitVo.setProfitRefund(Tools.getDouble(vphOrderProfitVo_refund.getProfit()));
                }
            }else if("mt".equals(orderProfitStatsDto.getPlatform())){
                //美团利润
                //总有效订单笔数
                orderProfitStatsDto.setOrderType(1);
                OrderProfitVo mtOrderProfitVo_valid = orderProfitStatsMapper.mtOrderProfitStats(orderProfitStatsDto);
                if(mtOrderProfitVo_valid != null){
                    //总佣金
                    profitVo.setCommission(Tools.getDouble(mtOrderProfitVo_valid.getCommission()));
                    //总利润
                    profitVo.setProfit(Tools.getDouble(mtOrderProfitVo_valid.getProfit()));
                    //买家分佣
                    profitVo.setCommissionUser(Tools.getDouble(mtOrderProfitVo_valid.getReUserBal()));
                    //总订单笔数
                    profitVo.setCount(Tools.getInteger(mtOrderProfitVo_valid.getCount()));
                    //总有效订单笔数
                    profitVo.setCountValid(Tools.getInteger(mtOrderProfitVo_valid.getCount()));
                    profitVo.setProfitValid(Tools.getDouble(mtOrderProfitVo_valid.getProfit()));
                }

                //总失效订单笔数
                orderProfitStatsDto.setOrderType(0);
                OrderProfitVo mtOrderProfitVo_invalid = orderProfitStatsMapper.mtOrderProfitStats(orderProfitStatsDto);
                if(mtOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    profitVo.setCountInvalid(Tools.getInteger(mtOrderProfitVo_invalid.getCount()));
                    profitVo.setProfitInvalid(Tools.getDouble(mtOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto.setOrderType(2);
                OrderProfitVo mtOrderProfitVo_refund = orderProfitStatsMapper.mtOrderProfitStats(orderProfitStatsDto);
                if(mtOrderProfitVo_refund != null){
                    //总维权订单笔数
                    profitVo.setCountRefund(Tools.getInteger(mtOrderProfitVo_refund.getCount()));
                    profitVo.setProfitRefund(Tools.getDouble(mtOrderProfitVo_refund.getProfit()));
                }
            }else if("elm".equals(orderProfitStatsDto.getPlatform())){
                //饿了么利润
                //总有效订单笔数
                orderProfitStatsDto.setOrderType(1);
                OrderProfitVo elmOrderProfitVo_valid = orderProfitStatsMapper.elmOrderProfitStats(orderProfitStatsDto);
                if(elmOrderProfitVo_valid != null){
                    //总佣金
                    profitVo.setCommission(Tools.getDouble(elmOrderProfitVo_valid.getCommission()));
                    //总利润
                    profitVo.setProfit(Tools.getDouble(elmOrderProfitVo_valid.getProfit()));
                    //买家分佣
                    profitVo.setCommissionUser(Tools.getDouble(elmOrderProfitVo_valid.getReUserBal()));
                    //总订单笔数
                    profitVo.setCount(Tools.getInteger(elmOrderProfitVo_valid.getCount()));
                    //总有效订单笔数
                    profitVo.setCountValid(Tools.getInteger(elmOrderProfitVo_valid.getCount()));
                    profitVo.setProfitValid(Tools.getDouble(elmOrderProfitVo_valid.getProfit()));
                }

                //总失效订单笔数
                orderProfitStatsDto.setOrderType(0);
                OrderProfitVo elmOrderProfitVo_invalid = orderProfitStatsMapper.elmOrderProfitStats(orderProfitStatsDto);
                if(elmOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    profitVo.setCountInvalid(Tools.getInteger(elmOrderProfitVo_invalid.getCount()));
                    profitVo.setProfitInvalid(Tools.getDouble(elmOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto.setOrderType(2);
                OrderProfitVo elmOrderProfitVo_refund = orderProfitStatsMapper.elmOrderProfitStats(orderProfitStatsDto);
                if(elmOrderProfitVo_refund != null){
                    //总维权订单笔数
                    profitVo.setCountRefund(Tools.getInteger(elmOrderProfitVo_refund.getCount()));
                    profitVo.setProfitRefund(Tools.getDouble(elmOrderProfitVo_refund.getProfit()));
                }
            }else if("dy".equals(orderProfitStatsDto.getPlatform())){
                //抖音利润
                //总有效订单笔数
                orderProfitStatsDto.setOrderType(1);
                OrderProfitVo dyOrderProfitVo_valid = orderProfitStatsMapper.dyOrderProfitStats(orderProfitStatsDto);
                if(dyOrderProfitVo_valid != null){
                    //总佣金
                    profitVo.setCommission(Tools.getDouble(dyOrderProfitVo_valid.getCommission()));
                    //总利润
                    profitVo.setProfit(Tools.getDouble(dyOrderProfitVo_valid.getProfit()));
                    //买家分佣
                    profitVo.setCommissionUser(Tools.getDouble(dyOrderProfitVo_valid.getReUserBal()));
                    //总订单笔数
                    profitVo.setCount(Tools.getInteger(dyOrderProfitVo_valid.getCount()));
                    //总有效订单笔数
                    profitVo.setCountValid(Tools.getInteger(dyOrderProfitVo_valid.getCount()));
                    profitVo.setProfitValid(Tools.getDouble(dyOrderProfitVo_valid.getProfit()));
                }

                //总失效订单笔数
                orderProfitStatsDto.setOrderType(0);
                OrderProfitVo dyOrderProfitVo_invalid = orderProfitStatsMapper.dyOrderProfitStats(orderProfitStatsDto);
                if(dyOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    profitVo.setCountInvalid(Tools.getInteger(dyOrderProfitVo_invalid.getCount()));
                    profitVo.setProfitInvalid(Tools.getDouble(dyOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto.setOrderType(2);
                OrderProfitVo dyOrderProfitVo_refund = orderProfitStatsMapper.dyOrderProfitStats(orderProfitStatsDto);
                if(dyOrderProfitVo_refund != null){
                    //总维权订单笔数
                    profitVo.setCountRefund(Tools.getInteger(dyOrderProfitVo_refund.getCount()));
                    profitVo.setProfitRefund(Tools.getDouble(dyOrderProfitVo_refund.getProfit()));
                }
            }
        }else{//所有平台
            double commission_total = 0.00d;
            double profit_total = 0.00d;
            double commissionUser_total = 0.00d;
            int count_total = 0;
            int countValid_total = 0;
            double profitValid_total = 0.00d;
            int countInvalid_total = 0;
            double profitInvalid_total = 0.00d;
            int countRefund_total = 0;
            double profitRefund_total = 0.00d;

            //淘宝利润
            if(1==1){
                OrderProfitStatsDto orderProfitStatsDto_tb = new OrderProfitStatsDto();
                BeanUtils.copyProperties(orderProfitStatsDto, orderProfitStatsDto_tb);
                //总有效订单笔数
                orderProfitStatsDto_tb.setOrderType(1);
                OrderProfitVo tbOrderProfitVo_valid = orderProfitStatsMapper.tbOrderProfitStats(orderProfitStatsDto_tb);
                if(tbOrderProfitVo_valid != null){
                    //总佣金
                    double commission = Tools.getDouble(tbOrderProfitVo_valid.getCommission());
                    //总利润
                    double profit = Tools.getDouble(tbOrderProfitVo_valid.getProfit());
                    //买家分佣
                    double commissionUser = Tools.getDouble(tbOrderProfitVo_valid.getReUserBal());
                    //总笔数
                    int count = Tools.getInteger(tbOrderProfitVo_valid.getCount());

                    //累加
                    commission_total =  com.uzai.common.utils.Math.add(commission_total, commission);
                    profit_total = com.uzai.common.utils.Math.add(profit_total, profit);
                    commissionUser_total = com.uzai.common.utils.Math.add(commissionUser_total, commissionUser);
                    count_total = count_total + count;

                    //总有效订单笔数
                    countValid_total =  countValid_total + count;
                    profitValid_total = com.uzai.common.utils.Math.add(profitValid_total, profit);
                }

                //总失效订单笔数
                orderProfitStatsDto_tb.setOrderType(0);
                OrderProfitVo tbOrderProfitVo_invalid = orderProfitStatsMapper.tbOrderProfitStats(orderProfitStatsDto_tb);
                if(tbOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    countInvalid_total =  countInvalid_total + Tools.getInteger(tbOrderProfitVo_invalid.getCount());
                    profitInvalid_total = com.uzai.common.utils.Math.add(profitInvalid_total, Tools.getDouble(tbOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto_tb.setOrderType(2);
                OrderProfitVo tbOrderProfitVo_refund = orderProfitStatsMapper.tbOrderProfitStats(orderProfitStatsDto_tb);
                if(tbOrderProfitVo_refund != null){
                    //总维权订单笔数
                    countRefund_total =  countRefund_total + Tools.getInteger(tbOrderProfitVo_refund.getCount());
                    profitRefund_total = com.uzai.common.utils.Math.add(profitRefund_total, Tools.getDouble(tbOrderProfitVo_refund.getProfit()));
                }
            }

            //京东利润
            if(1==1){
                OrderProfitStatsDto orderProfitStatsDto_jd = new OrderProfitStatsDto();
                BeanUtils.copyProperties(orderProfitStatsDto, orderProfitStatsDto_jd);

                //总有效订单笔数
                orderProfitStatsDto_jd.setOrderType(1);
                OrderProfitVo jdOrderProfitVo_valid = orderProfitStatsMapper.jdOrderProfitStats(orderProfitStatsDto_jd);
                if(jdOrderProfitVo_valid != null){

                    //总佣金
                    double commission = Tools.getDouble(jdOrderProfitVo_valid.getCommission());
                    //总利润
                    double profit = Tools.getDouble(jdOrderProfitVo_valid.getProfit());
                    //买家分佣
                    double commissionUser = Tools.getDouble(jdOrderProfitVo_valid.getReUserBal());
                    //总笔数
                    int count = Tools.getInteger(jdOrderProfitVo_valid.getCount());

                    //累加
                    commission_total =  com.uzai.common.utils.Math.add(commission_total, commission);
                    profit_total = com.uzai.common.utils.Math.add(profit_total, profit);
                    commissionUser_total = com.uzai.common.utils.Math.add(commissionUser_total, commissionUser);
                    count_total = count_total + count;

                    //总有效订单笔数
                    countValid_total =  countValid_total + count;
                    profitValid_total = com.uzai.common.utils.Math.add(profitValid_total, profit);
                }

                //总失效订单笔数
                orderProfitStatsDto_jd.setOrderType(0);
                OrderProfitVo jdOrderProfitVo_invalid = orderProfitStatsMapper.jdOrderProfitStats(orderProfitStatsDto_jd);
                if(jdOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    countInvalid_total =  countInvalid_total + Tools.getInteger(jdOrderProfitVo_invalid.getCount());
                    profitInvalid_total = com.uzai.common.utils.Math.add(profitInvalid_total, Tools.getDouble(jdOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto_jd.setOrderType(2);
                OrderProfitVo jdOrderProfitVo_refund = orderProfitStatsMapper.jdOrderProfitStats(orderProfitStatsDto_jd);
                if(jdOrderProfitVo_refund != null){
                    //总维权订单笔数
                    countRefund_total =  countRefund_total + Tools.getInteger(jdOrderProfitVo_refund.getCount());
                    profitRefund_total = com.uzai.common.utils.Math.add(profitRefund_total, Tools.getDouble(jdOrderProfitVo_refund.getProfit()));
                }
            }


            //拼多多利润
            if(1==1){
                OrderProfitStatsDto orderProfitStatsDto_pdd = new OrderProfitStatsDto();
                BeanUtils.copyProperties(orderProfitStatsDto, orderProfitStatsDto_pdd);

                //总有效订单笔数
                orderProfitStatsDto_pdd.setOrderType(1);
                OrderProfitVo pddOrderProfitVo_valid = orderProfitStatsMapper.pddOrderProfitStats(orderProfitStatsDto_pdd);
                if(pddOrderProfitVo_valid != null){
                    //总佣金
                    double commission = Tools.getDouble(pddOrderProfitVo_valid.getCommission());
                    //总利润
                    double profit = Tools.getDouble(pddOrderProfitVo_valid.getProfit());
                    //买家分佣
                    double commissionUser = Tools.getDouble(pddOrderProfitVo_valid.getReUserBal());
                    //总笔数
                    int count = Tools.getInteger(pddOrderProfitVo_valid.getCount());
                    //累加
                    commission_total =  com.uzai.common.utils.Math.add(commission_total, commission);
                    profit_total = com.uzai.common.utils.Math.add(profit_total, profit);
                    commissionUser_total = com.uzai.common.utils.Math.add(commissionUser_total, commissionUser);
                    count_total = count_total + count;
                    //总有效订单笔数
                    countValid_total =  countValid_total + count;
                    profitValid_total = com.uzai.common.utils.Math.add(profitValid_total, profit);
                }

                //总失效订单笔数
                orderProfitStatsDto_pdd.setOrderType(0);
                OrderProfitVo pddOrderProfitVo_invalid = orderProfitStatsMapper.pddOrderProfitStats(orderProfitStatsDto_pdd);
                if(pddOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    countInvalid_total =  countInvalid_total + Tools.getInteger(pddOrderProfitVo_invalid.getCount());
                    profitInvalid_total = com.uzai.common.utils.Math.add(profitInvalid_total, Tools.getDouble(pddOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto_pdd.setOrderType(2);
                OrderProfitVo pddOrderProfitVo_refund = orderProfitStatsMapper.pddOrderProfitStats(orderProfitStatsDto_pdd);
                if(pddOrderProfitVo_refund != null){
                    //总维权订单笔数
                    countRefund_total =  countRefund_total + Tools.getInteger(pddOrderProfitVo_refund.getCount());
                    profitRefund_total = com.uzai.common.utils.Math.add(profitRefund_total, Tools.getDouble(pddOrderProfitVo_refund.getProfit()));
                }
            }

            //唯品会利润
            if(1==1){
                OrderProfitStatsDto orderProfitStatsDto_vph = new OrderProfitStatsDto();
                BeanUtils.copyProperties(orderProfitStatsDto, orderProfitStatsDto_vph);
                //总有效订单笔数
                orderProfitStatsDto_vph.setOrderType(1);
                OrderProfitVo vphOrderProfitVo_valid = orderProfitStatsMapper.vphOrderProfitStats(orderProfitStatsDto_vph);
                if(vphOrderProfitVo_valid != null){
                    //总佣金
                    double commission = Tools.getDouble(vphOrderProfitVo_valid.getCommission());
                    //总利润
                    double profit = Tools.getDouble(vphOrderProfitVo_valid.getProfit());
                    //买家分佣
                    double commissionUser = Tools.getDouble(vphOrderProfitVo_valid.getReUserBal());
                    //总笔数
                    int count = Tools.getInteger(vphOrderProfitVo_valid.getCount());
                    //累加
                    commission_total =  com.uzai.common.utils.Math.add(commission_total, commission);
                    profit_total = com.uzai.common.utils.Math.add(profit_total, profit);
                    commissionUser_total = com.uzai.common.utils.Math.add(commissionUser_total, commissionUser);
                    count_total = count_total + count;
                    //总有效订单笔数
                    countValid_total =  countValid_total + count;
                    profitValid_total = com.uzai.common.utils.Math.add(profitValid_total, profit);
                }

                //总失效订单笔数
                orderProfitStatsDto_vph.setOrderType(0);
                OrderProfitVo vphOrderProfitVo_invalid = orderProfitStatsMapper.vphOrderProfitStats(orderProfitStatsDto_vph);
                if(vphOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    countInvalid_total =  countInvalid_total + Tools.getInteger(vphOrderProfitVo_invalid.getCount());
                    profitInvalid_total = com.uzai.common.utils.Math.add(profitInvalid_total, Tools.getDouble(vphOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto_vph.setOrderType(2);
                OrderProfitVo vphOrderProfitVo_refund = orderProfitStatsMapper.vphOrderProfitStats(orderProfitStatsDto_vph);
                if(vphOrderProfitVo_refund != null){
                    //总维权订单笔数
                    countRefund_total =  countRefund_total + Tools.getInteger(vphOrderProfitVo_refund.getCount());
                    profitRefund_total = com.uzai.common.utils.Math.add(profitRefund_total, Tools.getDouble(vphOrderProfitVo_refund.getProfit()));
                }
            }

            //美团利润
            if(1==1){
                OrderProfitStatsDto orderProfitStatsDto_mt = new OrderProfitStatsDto();
                BeanUtils.copyProperties(orderProfitStatsDto, orderProfitStatsDto_mt);
                //总有效订单笔数
                orderProfitStatsDto_mt.setOrderType(1);
                OrderProfitVo mtOrderProfitVo_valid = orderProfitStatsMapper.mtOrderProfitStats(orderProfitStatsDto_mt);
                if(mtOrderProfitVo_valid != null){
                    //总佣金
                    double commission = Tools.getDouble(mtOrderProfitVo_valid.getCommission());
                    //总利润
                    double profit = Tools.getDouble(mtOrderProfitVo_valid.getProfit());
                    //买家分佣
                    double commissionUser = Tools.getDouble(mtOrderProfitVo_valid.getReUserBal());
                    //总笔数
                    int count = Tools.getInteger(mtOrderProfitVo_valid.getCount());
                    //累加
                    commission_total =  com.uzai.common.utils.Math.add(commission_total, commission);
                    profit_total = com.uzai.common.utils.Math.add(profit_total, profit);
                    commissionUser_total = com.uzai.common.utils.Math.add(commissionUser_total, commissionUser);
                    count_total = count_total + count;
                    //总有效订单笔数
                    countValid_total =  countValid_total + count;
                    profitValid_total = com.uzai.common.utils.Math.add(profitValid_total, profit);
                }

                //总失效订单笔数
                orderProfitStatsDto_mt.setOrderType(0);
                OrderProfitVo mtOrderProfitVo_invalid = orderProfitStatsMapper.mtOrderProfitStats(orderProfitStatsDto_mt);
                if(mtOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    countInvalid_total =  countInvalid_total + Tools.getInteger(mtOrderProfitVo_invalid.getCount());
                    profitInvalid_total = com.uzai.common.utils.Math.add(profitInvalid_total, Tools.getDouble(mtOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto_mt.setOrderType(2);
                OrderProfitVo mtOrderProfitVo_refund = orderProfitStatsMapper.mtOrderProfitStats(orderProfitStatsDto_mt);
                if(mtOrderProfitVo_refund != null){
                    //总维权订单笔数
                    countRefund_total =  countRefund_total + Tools.getInteger(mtOrderProfitVo_refund.getCount());
                    profitRefund_total = com.uzai.common.utils.Math.add(profitRefund_total, Tools.getDouble(mtOrderProfitVo_refund.getProfit()));
                }
            }

            //饿了么利润
            if(1==1){
                OrderProfitStatsDto orderProfitStatsDto_elm = new OrderProfitStatsDto();
                BeanUtils.copyProperties(orderProfitStatsDto, orderProfitStatsDto_elm);
                //总有效订单笔数
                orderProfitStatsDto_elm.setOrderType(1);
                OrderProfitVo elmOrderProfitVo_valid = orderProfitStatsMapper.elmOrderProfitStats(orderProfitStatsDto_elm);
                if(elmOrderProfitVo_valid != null){
                    //总佣金
                    double commission = Tools.getDouble(elmOrderProfitVo_valid.getCommission());
                    //总利润
                    double profit = Tools.getDouble(elmOrderProfitVo_valid.getProfit());
                    //买家分佣
                    double commissionUser = Tools.getDouble(elmOrderProfitVo_valid.getReUserBal());
                    //总笔数
                    int count = Tools.getInteger(elmOrderProfitVo_valid.getCount());
                    //累加
                    commission_total =  com.uzai.common.utils.Math.add(commission_total, commission);
                    profit_total = com.uzai.common.utils.Math.add(profit_total, profit);
                    commissionUser_total = com.uzai.common.utils.Math.add(commissionUser_total, commissionUser);
                    count_total = count_total + count;
                    //总有效订单笔数
                    countValid_total =  countValid_total + count;
                    profitValid_total = com.uzai.common.utils.Math.add(profitValid_total, profit);
                }

                //总失效订单笔数
                orderProfitStatsDto_elm.setOrderType(0);
                OrderProfitVo elmOrderProfitVo_invalid = orderProfitStatsMapper.elmOrderProfitStats(orderProfitStatsDto_elm);
                if(elmOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    countInvalid_total =  countInvalid_total + Tools.getInteger(elmOrderProfitVo_invalid.getCount());
                    profitInvalid_total = com.uzai.common.utils.Math.add(profitInvalid_total, Tools.getDouble(elmOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto_elm.setOrderType(2);
                OrderProfitVo elmOrderProfitVo_refund = orderProfitStatsMapper.elmOrderProfitStats(orderProfitStatsDto_elm);
                if(elmOrderProfitVo_refund != null){
                    //总维权订单笔数
                    countRefund_total =  countRefund_total + Tools.getInteger(elmOrderProfitVo_refund.getCount());
                    profitRefund_total = com.uzai.common.utils.Math.add(profitRefund_total, Tools.getDouble(elmOrderProfitVo_refund.getProfit()));
                }
            }

            //抖音利润
            if(1==1){
                OrderProfitStatsDto orderProfitStatsDto_dy = new OrderProfitStatsDto();
                BeanUtils.copyProperties(orderProfitStatsDto, orderProfitStatsDto_dy);
                //总有效订单笔数
                orderProfitStatsDto_dy.setOrderType(1);
                OrderProfitVo dyOrderProfitVo_valid = orderProfitStatsMapper.dyOrderProfitStats(orderProfitStatsDto_dy);
                if(dyOrderProfitVo_valid != null){
                    //总佣金
                    double commission = Tools.getDouble(dyOrderProfitVo_valid.getCommission());
                    //总利润
                    double profit = Tools.getDouble(dyOrderProfitVo_valid.getProfit());
                    //买家分佣
                    double commissionUser = Tools.getDouble(dyOrderProfitVo_valid.getReUserBal());
                    //总笔数
                    int count = Tools.getInteger(dyOrderProfitVo_valid.getCount());
                    //累加
                    commission_total =  com.uzai.common.utils.Math.add(commission_total, commission);
                    profit_total = com.uzai.common.utils.Math.add(profit_total, profit);
                    commissionUser_total = com.uzai.common.utils.Math.add(commissionUser_total, commissionUser);
                    count_total = count_total + count;
                    //总有效订单笔数
                    countValid_total =  countValid_total + count;
                    profitValid_total = com.uzai.common.utils.Math.add(profitValid_total, profit);
                }

                //总失效订单笔数
                orderProfitStatsDto_dy.setOrderType(0);
                OrderProfitVo dyOrderProfitVo_invalid = orderProfitStatsMapper.dyOrderProfitStats(orderProfitStatsDto_dy);
                if(dyOrderProfitVo_invalid != null){
                    //总失效订单笔数
                    countInvalid_total =  countInvalid_total + Tools.getInteger(dyOrderProfitVo_invalid.getCount());
                    profitInvalid_total = com.uzai.common.utils.Math.add(profitInvalid_total, Tools.getDouble(dyOrderProfitVo_invalid.getProfit()));
                }

                //总维权订单笔数
                orderProfitStatsDto_dy.setOrderType(2);
                OrderProfitVo dyOrderProfitVo_refund = orderProfitStatsMapper.dyOrderProfitStats(orderProfitStatsDto_dy);
                if(dyOrderProfitVo_refund != null){
                    //总维权订单笔数
                    countRefund_total =  countRefund_total + Tools.getInteger(dyOrderProfitVo_refund.getCount());
                    profitRefund_total = com.uzai.common.utils.Math.add(profitRefund_total, Tools.getDouble(dyOrderProfitVo_refund.getProfit()));
                }
            }

            //复制所有平台
            //总佣金
            profitVo.setCommission(commission_total);
            //总利润
            profitVo.setProfit(profit_total);
            profitVo.setCommissionUser(commissionUser_total);
            //总订单笔数
            profitVo.setCount(count_total);
            //总有效订单笔数
            profitVo.setCountValid(countValid_total);
            profitVo.setProfitValid(profitValid_total);
            //总失效订单笔数
            profitVo.setCountInvalid(countInvalid_total);
            profitVo.setProfitInvalid(profitInvalid_total);
            //总维权订单笔数
            profitVo.setCountRefund(countRefund_total);
            profitVo.setProfitRefund(profitRefund_total);

        }

        return profitVo;
    }

    /**
     * 分页查询订单收益统计
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param orderProfitStatsQuery
     * @return null
     */
    public Page<OrderProfitPageVo> findOrderProfitStatsPage(OrderProfitStatsQuery orderProfitStatsQuery){
        //返回到页面分页对象
        Page<OrderProfitPageVo> pageVo = new Page<>();
        orderProfitStatsQuery.convert(pageVo);

        Long merId = orderProfitStatsQuery.getMerId();
        String deviceType = orderProfitStatsQuery.getDeviceType();
        if(merId == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "系统参数错误");
        }

        if(StringUtils.isBlank(deviceType)){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择设备类型");
        }

        //手动处理机器人分页查询-如果是机器人昵称，则按照中文gbk排序
        List<OrderItem> orderItemList = orderProfitStatsQuery.getOrderItemList();
        if(orderItemList != null && orderItemList.size() > 0){
            for (OrderItem orderItem :orderItemList){
                if("device_name".equals(orderItem.getColumn())){
                    orderItem.setColumn(" convert( " + orderItem.getColumn() + " using gbk) ");
                }
            }
        }

        //新建返回到页面的LIST对象
        List<OrderProfitPageVo> orderProfitPageVoList = new ArrayList<>();

        //个人微信
        if(DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceType)){
            Page<DeviceWechatInfo> page = new Page<>();
            orderProfitStatsQuery.convert(page);
            DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
            BeanUtils.copyProperties(orderProfitStatsQuery, deviceWechatInfoQuery);
            deviceWechatInfoQuery.setIdList(orderProfitStatsQuery.getDeviceUniqueIdList());
            ArrayList<DeviceWechatInfo> deviceWechatInfoList =  deviceWechatInfoMapper.findByList(page, deviceWechatInfoQuery);
            BeanUtils.copyProperties(page, pageVo);
            if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0){
                for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList){
                    OrderProfitPageVo orderProfitPageVo = new OrderProfitPageVo();
                    orderProfitPageVo.setDeviceUniqueId(deviceWechatInfo.getId());
                    orderProfitPageVo.setDeviceName(deviceWechatInfo.getDeviceName());
                    orderProfitPageVo.setDeviceRemark(deviceWechatInfo.getDeviceRemark());
                    orderProfitPageVoList.add(orderProfitPageVo);
                }
            }
        }else if(DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceType)){
            Page<DeviceWxworkInfo> page = new Page<>();
            orderProfitStatsQuery.convert(page);
            DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
            BeanUtils.copyProperties(orderProfitStatsQuery, deviceWxworkInfoQuery);
            deviceWxworkInfoQuery.setIdList(orderProfitStatsQuery.getDeviceUniqueIdList());
            ArrayList<DeviceWxworkInfo> deviceWxworkInfoList =  deviceWxworkInfoMapper.findByList(page, deviceWxworkInfoQuery);
            BeanUtils.copyProperties(page, pageVo);
            if(deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0){
                for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList){
                    OrderProfitPageVo orderProfitPageVo = new OrderProfitPageVo();
                    orderProfitPageVo.setDeviceUniqueId(deviceWxworkInfo.getId());
                    orderProfitPageVo.setDeviceName(deviceWxworkInfo.getDeviceName());
                    orderProfitPageVo.setDeviceRemark(deviceWxworkInfo.getDeviceRemark());
                    orderProfitPageVoList.add(orderProfitPageVo);
                }
            }
        }else if(DeviceTypeEnum.WX_PUB.getKey().equals(deviceType)){
            Page<WxpubInfoEntity> page = new Page<>();
            orderProfitStatsQuery.convert(page);
            WxpubInfoListQuery wxpubInfoListQuery = new WxpubInfoListQuery();
            BeanUtils.copyProperties(orderProfitStatsQuery, wxpubInfoListQuery);
            wxpubInfoListQuery.setIdList(orderProfitStatsQuery.getDeviceUniqueIdList());
            List<WxpubInfoEntity> wxpubInfoEntityList =  wxpubInfoMapper.queryList(page, wxpubInfoListQuery);
            BeanUtils.copyProperties(page, pageVo);
            if(wxpubInfoEntityList != null && wxpubInfoEntityList.size() > 0){
                for (WxpubInfoEntity wxpubInfoEntity : wxpubInfoEntityList){
                    OrderProfitPageVo orderProfitPageVo = new OrderProfitPageVo();
                    orderProfitPageVo.setDeviceUniqueId(wxpubInfoEntity.getId());
                    orderProfitPageVo.setDeviceName(wxpubInfoEntity.getDeviceName());
                    orderProfitPageVo.setDeviceRemark(wxpubInfoEntity.getDeviceRemark());
                    List<Long> deviceUniqueIdList = new ArrayList<>();
                    deviceUniqueIdList.add(orderProfitPageVo.getDeviceUniqueId());

                    //已绑定买家
                    OrderProfitStatsDto orderProfitStatsDto_bindUser = new OrderProfitStatsDto();
                    BeanUtils.copyProperties(orderProfitStatsQuery, orderProfitStatsDto_bindUser);
                    orderProfitStatsDto_bindUser.setDeviceUniqueIdList(deviceUniqueIdList);
                    orderProfitStatsDto_bindUser.setBindUser(1);
                    OrderProfitStatsVo bindUser = findOrderProfitStats(orderProfitStatsDto_bindUser);
                    orderProfitPageVo.setBindUser(bindUser);

                    //未绑定买家
                    OrderProfitStatsDto orderProfitStatsDto_noBindUser = new OrderProfitStatsDto();
                    BeanUtils.copyProperties(orderProfitStatsQuery, orderProfitStatsDto_noBindUser);
                    orderProfitStatsDto_noBindUser.setDeviceUniqueIdList(deviceUniqueIdList);
                    orderProfitStatsDto_noBindUser.setBindUser(0);
                    OrderProfitStatsVo noBindUser = findOrderProfitStats(orderProfitStatsDto_noBindUser);
                    orderProfitPageVo.setNoBindUser(noBindUser);

                    //所有买家
                    OrderProfitStatsDto orderProfitStatsDto_allUser = new OrderProfitStatsDto();
                    BeanUtils.copyProperties(orderProfitStatsQuery, orderProfitStatsDto_allUser);
                    orderProfitStatsDto_allUser.setDeviceUniqueIdList(deviceUniqueIdList);
                    OrderProfitStatsVo aallUser = findOrderProfitStats(orderProfitStatsDto_allUser);
                    orderProfitPageVo.setAllUser(aallUser);

                    orderProfitPageVoList.add(orderProfitPageVo);
                }
            }
        }
        pageVo.setRecords(orderProfitPageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }


}
